/**
 * 
 */
package belote.webapp.struts.user;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import belote.webapp.WebappConstants;

/**
 * @author Gwenn
 * 
 */
public class LogoutAction extends Action {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ActionForward execute(ActionMapping pMapping, ActionForm pForm, HttpServletRequest pRequest, HttpServletResponse pResponse) throws Exception {

		// On supprime l'utilisateur en session
		HttpSession session = pRequest.getSession();
		session.removeAttribute(WebappConstants.SESSION.USER);

		// On supprime le cookie 'remember'
		Cookie[] cookies = pRequest.getCookies();
		for (Cookie cookie : cookies) {
			if (WebappConstants.COOKIE.REMEMBER.equals(cookie.getName())) {
				cookie.setMaxAge(-1);
				cookie.setPath("/");
				cookie.setValue("");
				pResponse.addCookie(cookie);
			}
		}

		return pMapping.findForward(WebappConstants.FORWARD.SUCCESS);
	}
}
